<?php

namespace common\models;

use common\helpers\commonApi;
use common\helpers\PageApi;
use common\helpers\TransformApi;
use Yii;

/**
 * This is the model class for table "{{%equity}}".
 *
 * @property int $id
 * @property string|null $name 名称
 * @property string|null $main_img 主图
 * @property string|null $img 图片
 * @property string|null $address 地址
 * @property string|null $long 经度
 * @property string|null $lat 纬度
 * @property string|null $feature 特色
 * @property string|null $trait 特点
 * @property int|null $c_id 分类id
 * @property string|null $desc 描述
 * @property string|null $logo logo
 * @property string|null $mobile 手机号
 * @property int|null $status 状态
 * @property int|null $is_boutique 精品
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property int|null $views 浏览量
 * @property int|null $sort 排序
 */
class Equity extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%equity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['feature', 'trait', 'desc'], 'string'],
            [['c_id', 'status', 'is_boutique', 'views', 'sort'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'main_img', 'img', 'address', 'long', 'lat', 'logo', 'mobile'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'main_img' => '主图',
            'img' => '图片',
            'address' => '地址',
            'long' => '经度',
            'lat' => '纬度',
            'feature' => '特色',
            'trait' => '特点',
            'c_id' => '分类id',
            'desc' => '描述',
            'logo' => 'logo',
            'mobile' => '手机号',
            'status' => '状态',
            'is_boutique' => '精品',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'views' => '浏览量',
            'sort' => '排序',
        ];
    }

    /**
     * 获取列表
     * @param $where
     * @param $page
     * @param $long
     * @param $lat
     * @return array
     */
    public function getList($where,$page,$long,$lat)
    {
        $query = Equity::find()
            ->where($where)
            ->orderBy(['sort'=>SORT_DESC,'create_time'=>SORT_DESC])
            ->select(['id as e_id','name','main_img','feature','trait','long','lat']);
        $data = PageApi::apiPage($query,$page);
        foreach ($data['data'] as $key=>&$datum){
            $datum['main_img'] = commonApi::getDomain($datum['main_img']);
            $datum['feature'] = explode(',',$datum['feature']);
            $datum['trait'] = explode(',',$datum['trait']);
            $datum['distance'] = '';
            if ($long&&$lat){
                $distance = TransformApi::getDistance($datum['long'],$datum['lat'],$long,$lat);
                $datum['distance'] = $distance;
            }
            unset($data['data'][$key]['long']);
            unset($data['data'][$key]['lat']);
        }
        return commonApi::message('成功',true,$data);
    }
}
